package com.situ.fly.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.ui.Model;

public class BaseVerCode {
	/**
	 * 验证验证码
	 * 
	 * @param vercode
	 * @param session
	 * @param model 验证错误信息在model中
	 * @return true: 验证通过,  false: 验证失败
	 */
	public static boolean checkVerCode(String vercode, HttpSession session, Model model) {
		// 验证验证码
		// 获取生成的验证码
		String genVercode = (String) session.getAttribute("verCode");
		if (!genVercode.equals(vercode.toUpperCase())) {
			// 验证码不正确
			model.addAttribute("error", "验证码错误");
			return false;
		}
		
		// 验证时间有效性
		Date genTime = (Date) session.getAttribute("genTime");
		Date curTime = new Date();
		
		if ( (curTime.getTime() - genTime.getTime()) > 10*60*1000) {
			// 验证码超时
			model.addAttribute("error", "验证码超时");
			return false;
		}
		
		return true;
	}
}
